package com.tepestech.pfautocard.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.tepestech.pfautocard.R;
import com.tepestech.pfautocard.data.Skill;
import com.tepestech.pfautocard.data.Statics;

import java.util.ArrayList;

public class SkillFragment extends Fragment {
    private static final ArrayList<Skill> skillList = new ArrayList<>();

    static {
        skillList.add(new Skill("特技动作", Statics.DEX));
        skillList.add(new Skill("估价", Statics.INT));
        skillList.add(new Skill("唬弄", Statics.CHA));
        skillList.add(new Skill("攀爬", Statics.STR));
        skillList.add(new Skill("工艺:", true, true, Statics.INT));
        skillList.add(new Skill("工艺:", true, true, Statics.INT));
        skillList.add(new Skill("工艺:", true, true, Statics.INT));
        skillList.add(new Skill("交涉", Statics.CHA));
        skillList.add(new Skill("解除装置*", false, false, Statics.DEX));
        skillList.add(new Skill("易容", Statics.CHA));
        skillList.add(new Skill("逃脱", Statics.DEX));
        skillList.add(new Skill("飞行", Statics.DEX));
        skillList.add(new Skill("驯养动物*", false, false, Statics.CHA));
        skillList.add(new Skill("医疗", Statics.WIS));
        skillList.add(new Skill("威吓", Statics.CHA));
        skillList.add(new Skill("知识:神秘*", false, false, Statics.INT));
        skillList.add(new Skill("知识:地下城*", false, false, Statics.INT));
        skillList.add(new Skill("知识:工程*", false, false, Statics.INT));
        skillList.add(new Skill("知识:地理*", false, false, Statics.INT));
        skillList.add(new Skill("知识:历史*", false, false, Statics.INT));
        skillList.add(new Skill("知识:地方*", false, false, Statics.INT));
        skillList.add(new Skill("知识:自然*", false, false, Statics.INT));
        skillList.add(new Skill("知识:贵族*", false, false, Statics.INT));
        skillList.add(new Skill("知识:界域*", false, false, Statics.INT));
        skillList.add(new Skill("知识:宗教*", false, false, Statics.INT));
        skillList.add(new Skill("语言学*", false, false, Statics.INT));
        skillList.add(new Skill("察觉", Statics.WIS));
        skillList.add(new Skill("表演:", true, true, Statics.INT));
        skillList.add(new Skill("表演:", true, true, Statics.INT));
        skillList.add(new Skill("专业*:", false, true, Statics.INT));
        skillList.add(new Skill("专业*:", false, true, Statics.INT));
        skillList.add(new Skill("骑术", Statics.DEX));
        skillList.add(new Skill("察言观色", Statics.WIS));
        skillList.add(new Skill("巧手*", false, false, Statics.DEX));
        skillList.add(new Skill("观察法术*", false, false, Statics.INT));
        skillList.add(new Skill("隐匿", Statics.DEX));
        skillList.add(new Skill("野外求生", Statics.WIS));
        skillList.add(new Skill("游泳", Statics.STR));
        skillList.add(new Skill("使用魔法装置*", false, false, Statics.CHA));
    }

    private ListView skills;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_skill, container, false);
        skills = root.findViewById(R.id.skill_list);
        SkillAdapter adapter = new SkillAdapter();
        skills.setAdapter(adapter);
        skills.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Toast.makeText(getActivity(), "click!", Toast.LENGTH_SHORT).show();
            }
        });
        skills.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
                Toast.makeText(getActivity(), "long click!", Toast.LENGTH_SHORT).show();
                return true;
            }
        });
        return root;
    }

    private class SkillAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return skillList.size();
        }

        @Override
        public Skill getItem(int i) {
            return skillList.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @SuppressLint({"InflateParams", "SetTextI18n"})
        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            Holder holder;
            if (view == null) {
                holder = new Holder();
                view = LayoutInflater.from(getActivity()).inflate(R.layout.item_skill, null);
                holder.skill_name = view.findViewById(R.id.skill_name);
                holder.skill_levels = view.findViewById(R.id.skill_levels);
                holder.skill_point = view.findViewById(R.id.skill_point);
                view.setTag(holder);
            } else {
                holder = (Holder) view.getTag();
            }
            holder.skill_name.setText(getItem(i).getName());
            if (getItem(i).getPoint(0) == null) {
                holder.skill_levels.setText("");
                holder.skill_point.setText("-");
            } else {
                String levels = "";
                if (getItem(i).getClassSkill()) levels += "本职+";
                switch (getItem(i).getAbility()) {
                    case Statics.STR:
                        levels += "STR+";
                        break;
                    case Statics.DEX:
                        levels += "DEX+";
                        break;
                    case Statics.CON:
                        levels += "CON+";
                        break;
                    case Statics.INT:
                        levels += "INT+";
                        break;
                    case Statics.WIS:
                        levels += "WIS+";
                        break;
                    case Statics.CHA:
                        levels += "CHA+";
                        break;
                }
                levels += "lvl" + getItem(i).getLevel() + "+";
                levels += getItem(i).getAddons()+"=";
                holder.skill_levels.setText(levels);
                holder.skill_point.setText("" + getItem(i).getPoint(0));
            }

            return view;
        }
    }

    private static class Holder {
        TextView skill_name, skill_levels, skill_point;
    }

}
